﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace TreeViewForXamarin.Models
{
    /// <summary>
    /// 树节点数据
    /// </summary>
    public class TreeNode
    {
        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="treeNodeName">节点名称</param>
        /// <param name="data">源数据</param>
        /// <param name="parent">父级</param>
        public TreeNode(string treeNodeName, object data, TreeNode parent)
        {
            this.TreeNodeName = treeNodeName;
            this.Data = data;
            this.Parent = parent;
            this.ChildTreeNodes = new List<TreeNode>();
        }
        #endregion
        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="treeNodeName">节点名称</param>
        /// <param name="data">源数据</param>
        /// <param name="parent">父级</param>
        /// <param name="childTreeNodes">子节点集合</param>
        public TreeNode(string treeNodeName, object data, TreeNode parent,List<TreeNode> childTreeNodes)
        {
            this.TreeNodeName = treeNodeName;
            this.Data = data;
            this.Parent = parent;
            this.ChildTreeNodes = childTreeNodes == null ? new List<TreeNode>() : childTreeNodes;
        }
        #endregion

        #region # 属性
        /// <summary>
        /// 节点名称
        /// </summary>
        public string TreeNodeName { get; set; }
        /// <summary>
        /// 源数据
        /// </summary>
        public object Data { get; set; }
        /// <summary>
        /// 父级节点
        /// </summary>
        public TreeNode Parent { get; set; }
        /// <summary>
        /// 是否根节点
        /// </summary>
        public bool IsRoot { get { return this.Parent == null ? true : false; } }
        /// <summary>
        /// 是否有子节点
        /// </summary>
        public bool HasChildNode { get { return this.ChildTreeNodes == null ? false : this.ChildTreeNodes.Any(); } }
        /// <summary>
        /// 子节点总数
        /// </summary>
        public int ChildTreeNodeCount { get { return this.ChildTreeNodes == null ? 0 : this.ChildTreeNodes.Count; } }
        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpanded { get; set; }
        /// <summary>
        /// 子节点
        /// </summary>
        public List<TreeNode> ChildTreeNodes { get; set; } 
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsSelected { get; set; }
        #endregion

        #region # 方法
        #region # 获取层级——int GetLevel()
        /// <summary>
        /// 获取层级
        /// </summary>
        public int GetLevel()
        {
            int level = 0;
            TreeNode root = this;
            while (root.Parent != null)
            {
                root = root.Parent;
                level++;
            }
            return level;
        }
        #endregion

        #region # 设置子节点——void SetChildNodes(List<TreeNode> childTreeNodes)
        /// <summary>
        /// 设置子节点
        /// </summary>
        /// <param name="childTreeNodes">子节点集合</param>
        public void SetChildTreeNodes(List<TreeNode> childTreeNodes)
        {
            this.ChildTreeNodes = childTreeNodes == null ? new List<TreeNode>() : childTreeNodes;
        }
        #endregion

        #endregion
    }
}